package com.tuzhi.auth.sys.service.impl;

import com.tuzhi.auth.sys.jpa.entity.SystemLogEntity;
import com.tuzhi.auth.sys.jpa.repository.SystemLogRepo;
import com.tuzhi.auth.sys.service.SystemLogService;
import com.tuzhi.auth.sys.vo.query.QuerySystemLogVo;
import com.tuzhi.base.jpa.domain.Pager;
import com.tuzhi.base.jpa.healper.wenhao.TPredicateBuilder;
import com.tuzhi.base.jpa.healper.wenhao.TSpecifications;
import com.tuzhi.base.jpa.repository.BaseRepo;
import com.tuzhi.base.service.impl.AbsBaseService;
import com.tuzhi.base.web.vo.PageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class SystemLogServiceImpl extends AbsBaseService<SystemLogEntity, Long> implements SystemLogService {

    private final SystemLogRepo systemLogRepo;
    @Override
    protected BaseRepo getThisRepo() {
        return systemLogRepo;
    }

    @Override
    public Pager<SystemLogEntity> getPage(PageVO<QuerySystemLogVo> request) {
        QuerySystemLogVo reqData = request.getReqData();
        // 构建查询条件
        TPredicateBuilder<SystemLogEntity> query = TSpecifications.and();
        query.eq(StringUtils.isNotBlank(reqData.getModule()), SystemLogEntity::getModule, reqData.getModule());
        query.eq(StringUtils.isNotBlank(reqData.getLogType()), SystemLogEntity::getLogType, reqData.getLogType());
        query.eq(StringUtils.isNotBlank(reqData.getOperator()), SystemLogEntity::getOperator, reqData.getOperator());
        query.ge(ObjectUtils.isNotEmpty(reqData.getStartTime()), SystemLogEntity::getOperationTime, reqData.getStartTime());
        query.ge(ObjectUtils.isNotEmpty(reqData.getEndTime()), SystemLogEntity::getOperationTime, reqData.getEndTime());
        Pageable page = PageRequest.of(request.getPage(), request.getSize());
        Page<SystemLogEntity> pageResult = systemLogRepo.findAll(query.build(), page);
        // 构建返回对象
        Pager<SystemLogEntity> pager = new Pager<>();
        pager.setPage(request.getPage());
        pager.setSize(pageResult.getSize());
        pager.setTotalNum(pageResult.getTotalElements());
        pager.setData(pageResult.getContent());
        return pager;
    }
}
